package com.dg.lecheng.api.user.service;

import com.dg.lecheng.api.user.model.InfoUser;
import com.dg.lecheng.api.user.model.InfoUserAuth;
import com.dg.lecheng.api.user.model.InfoUserRole;
import com.dg.lecheng.api.user.repo.InfoUserAuthDao;
import com.dg.lecheng.base.core.service.CrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class InfoUserAuthService extends CrudService<InfoUserAuthDao, InfoUserAuth, Integer> {
    @Autowired
    private InfoUserRoleService infoUserRoleService;
    @Autowired
    private InfoRoleAuthService infoRoleAuthService;
    @Autowired
    private InfoUserService infoUserService;

    /**
     * 保存用户 和角色、资源的对应关系
     * 1. delete rbac_accountrole
     * 2. add rbac_accountrole
     * 3. delete rbac_accountresource
     * 4. add rbac_accountresource
     * @return
     */
    @Transactional("apiTM")
    public void saveRoleAuth(Map<String,Object> map) {
        int userId = (int)map.get("userId");
        int roleId = (int)map.get("roleId");
        List<Map> auths = (List<Map>)map.get("auths");

        // 1. Delete AccountRole By AccountId
        infoUserRoleService.removeByUserId(userId);
        // 2. Add AccountRole
        InfoUserRole infoUserRole = new InfoUserRole();
        infoUserRole.setUserId(userId);
        infoUserRole.setRoleId(roleId);
        infoUserRoleService.add(infoUserRole);
        // 3. Delete AccountResource By AccountId
        curdRepo.removeByUserId(userId);
        // 4. Add selected Resources
        if(auths != null && auths.size() > 0)
            curdRepo.addList(auths);
    }

    /**
     * 根据userId 删除对应关系
     * @param userId
     */
    public void removeByUserId(int userId){
        curdRepo.removeByUserId(userId);
    }

    /**
     * 根据authId 删除对应关系
     * @param authId
     */
    public void removeByAuthId(int authId){
        curdRepo.removeByAuthId(authId);
    }

    /**
     * 批量添加
     * @param list
     */
    public void addList(List<Map> list){
        curdRepo.addList(list);
    }

    /**
     * 查找用户的额外资源
     * @param userId
     * @return
     */
    public List<Map> findAuthByUserId(int userId){
        return curdRepo.findAuthByUserId(userId);
    }

    /**
     * 获取用户所有资源
     */
    public List<Map> getUserAuth(int userId){
        List<Map> result = new ArrayList<>();

        //1.现获取用户角色对应的资源
        List<InfoUserRole> infoUserRoles = infoUserRoleService.getByUserId(userId);
        if(infoUserRoles == null || infoUserRoles.size() == 0){
            return result;
        }
        result = infoRoleAuthService.findAuthByRoleId(infoUserRoles.get(0).getRoleId());

        //2.获取用户额外的资源
        List<Map> list = curdRepo.findAuthByUserId(userId);
        if(list != null && list.size() > 0){
            result.addAll(list);
        }
        return result;
    }

    /**
     * 获取用户所有资源
     * @param userName
     * @return
     */
    public List<Map> getUserAuthByUserName(String userName){
        if(StringUtils.isEmpty(userName))
            return null;

        InfoUser infoUser = infoUserService.getByUserName(userName);
        if(infoUser == null)
            return null;

        return getUserAuth(infoUser.getId());
    }
}